package jhm;

import robocode.HitByBulletEvent;
import robocode.HitRobotEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;

/**
 * CounterBot moves up and down the a wall and fires when it scans enemy.
 * 
 * @author Jarret
 * 
 */
public class CounterBot extends Robot {
  int direction; // direction robot is moving in, 0 is up/down, 1 is left/right

  /**
   * <code>run()</code> runs the Robot.
   */
  public void run() {
    double mapHeight = getBattleFieldHeight();// map height
    double mapWidth = getBattleFieldWidth(); // map width
    // turn robot to right wall
    turnLeft((getHeading() % 360) - 90);
    // move to closest side of map, 0 is right, 1 is left
    toSide();
    direction = 0;

    while (true) {
      if (direction == 0) {
        // move up and down along wall
        back(mapHeight);
        ahead(mapHeight);
      }
      else {
        // move left and right across map
        back(mapWidth);
        ahead(mapWidth);
      }

    }
  }

  /**
   * Shoots when robot is scanned. Power depends on distance between robots.
   * @param e event
   */
  public void onScannedRobot(ScannedRobotEvent e) {
    // out.println("Distance between: " + e.getDistance());
    if (e.getDistance() < 150) {
      fire(3);
    }
    else if (e.getDistance() < 300) {
      fire(2);
    }
    else {
      fire(1);
    }
    // scan 1 more time to see if robot is still in front of gun
    scan();
  }

  /**
   * If run into enemy, turn toward enemy and fire repeatedly.
   * @param e event
   */
  public void onHitRobot(HitRobotEvent e) {
    double enemyBearing = e.getBearing();
    // out.println("Collision direction: "+enemyBearing);
    if (enemyBearing > -50.0 && enemyBearing < 50) {
      turnRight(90);
      // sets direction check to opposite number
      if (direction == 0) {
        direction = 1;
      }
      else {
        direction = 0;
      }
    }
    if (enemyBearing < -130 || enemyBearing > 130) {
      turnLeft(90);
      // sets direction check to opposite number
      if (direction == 0) {
        direction = 1;
      }
      else {
        direction = 0;
      } // end else
    } // end if
  } // end onHitRobot

  /**
   * If shot in front of back by a bullet, turn in that direction and move.
   * @param e event
   */
  public void onHitByBullet(HitByBulletEvent e) {
    double enemyBearing = e.getBearing();
    // out.println(enemyBearing);
    // shot in the front
    if (enemyBearing > -30.0 && enemyBearing < 30.0) {
      turnRight(90);
      // sets direction check to opposite number
      if (direction == 0) {
        direction = 1;
      }
      else {
        direction = 0;
      }
    }
    // shot in the back
    if (enemyBearing < -150 || enemyBearing > 150) {
      turnLeft(90);
      // sets direction check to opposite number
      if (direction == 0) {
        direction = 1;
      }
      else {
        direction = 0;
      }
    }
  }

  /**
   * Moves to closest wall.
   */
  public void toSide() {
    // go to right side if true, else to left side
    if (getX() >= (getBattleFieldWidth() / 2)) {
      // head to right wall
      ahead(getBattleFieldWidth() - getX());
      // turn parallel wall
      turnLeft(90);
      // turn gun toward middle
      turnGunLeft(90);
    }
    else {
      // head to right wall
      back(getBattleFieldWidth() - getX());
      // turn parallel wall
      turnRight(90);
      // turn gun toward middle
      turnGunLeft(90);
    }
  }
}
